define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/offline_log/index' + location.search,
                    add_url: 'wanlshop/offline_log/add',
                    edit_url: 'wanlshop/offline_log/edit',
                    del_url: 'wanlshop/offline_log/del',
                    multi_url: 'wanlshop/offline_log/multi',
                    import_url: 'wanlshop/offline_log/import',
                    table: 'wanlshop_offline_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'), formatter: function (value, row, index) {
                            let user = row.user;
                            return `<di>ID：${user.id}</div><div>昵称：${user.nickname}</div>`;
                        }},
                        {field: 'order_id', title: __('Order_id'), formatter: function (value, row, index) {
                            let order = row.order;
                            return `<di>ID：${order.id}</div><div>订单号：${order.order_no
                            }</div>`;
                        }},
                        {field: 'shop_id', title: __('Shop_id'), formatter: function (value, row, index) {
                            let shop = row.shop;
                            return `<di>ID：${shop.id}</div><div><image src="${shop.avatar}" style="width:50px;height:50px;"/></div><div>名称：${shop.shopname
                            }</div>`;
                        }},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'payment_status', title: __('Payment_status'), searchList: {"0":__('Payment_status 0'),"1":__('Payment_status 1'),"2":__('Payment_status 2'),"3":__('Payment_status 3'),"4":__('Payment_status 4')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                            var that = $.extend({}, this);
                            var table = $(that.table).clone(true);
                            //隐藏删除按钮
                            $(table).data('operate-del', null);
                            $(table).data('operate-edit', null);
                            //根据状态判断是否要显示该按钮
                            
                            that.table = table;
                            return Table.api.formatter.operate.call(that, value,row,index)
                        },
                        buttons: [
                            {
                                name:'payment',
                                text:'标记付款',
                                icon: 'fa fa-',
                                classname: 'btn btn-xs btn-primary btn-ajax', //
                                confirm: '是否标记为已打款',
                                // btn-dialog 弹出窗
                                url: (row) => {
                                    return "wanlshop/offline_log/payment?id="+row.id 
                                }, //url 地址 这里是个用户列表
                                visible: function (row) {
                                    //true：显示 false：不显示 可以row自定义显示
                                    if (row.payment_status == '0') return true;
                                    return false;
                                },
                                success: function (data, rest) {
                                    //刷新当前页面
                                    table.bootstrapTable('refresh', {});
                                    return true;
                                }
                            }, 
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
